﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqProspectQuotations
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);

        public List<ProspectQuotations> GetQuotationsBySocode(string soCode,DateTime fromDate, DateTime toDate, int pageNo)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                var query = from quotation in db.ProspectQuotationEntities

                            where quotation.SOCode == soCode && fromDate < quotation.LastUpdated && toDate > quotation.LastUpdated
                            select quotation;



                var pageData = query.Skip((pageNo - 1) * pageSize).Take(pageSize);


                return pageData.Select(c => Mapper.ToProspectQuotatationsObject(c)).ToList();
                
            }
        }


        public List<ProspectQuotations> GetQuotationsByNo(string soCode, string quotationNum, DateTime fromDate, DateTime toDate)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                var query = from quotation in db.ProspectQuotationEntities

                            where quotation.QuotationNo == quotationNum && quotation.SOCode == soCode && fromDate < quotation.LastUpdated && toDate > quotation.LastUpdated
                            select quotation;


                return query.Select(c => Mapper.ToProspectQuotatationsObject(c)).ToList();
            }
        }


        public ProspectQuotations GetQuotation(string quotationNumber)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToProspectQuotatationsObject(db.ProspectQuotationEntities
                            .SingleOrDefault(p => p.QuotationNo == quotationNumber));
            }
        }


    }
}
